//
//  XSHomeViewController.swift
//  swiftWeibo
//
//  Created by 马雪山 on 2017/7/18.
//  Copyright © 2017年 xueshanma. All rights reserved.
//

import UIKit

//全局常量,使用private修饰, 防止到处都能访问
private let cellId = "cellId"

class XSHomeViewController: XSBaseViewController {
    
    //微博数据数组
    fileprivate lazy var statusList = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.green

    
    }
    
    override func loadData() {
        //模拟'延时'加载数据
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
            for i in 0..<2 {
                
                if self.isPullup{
                    self.statusList.append("上拉")
                }else{
                    //将数据插入到数组的顶部
                    self.statusList.insert(i.description, at: 0)
                }
                
               
            }
            
            self.tableView?.reloadData()
            
            self.isPullup = false
            //结束刷新控件
            self.refreshControl?.endRefreshing()
        }
        
       
        
    }
    
    
    func showFriends() {
        print(#function)
//        let vc = XSDemoVC()
//        navigationController?.pushViewController(vc, animated: true)
    }

    

}

//MARK: 表格数据源
extension XSHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.textLabel?.text = statusList[indexPath.row]
        
        return cell
    }
    
}

//MARK: 设置界面
extension XSHomeViewController {

    override func setupUI() {
        super.setupUI()

        //使用抽取的方法, navItem是自定制的
        navItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", target: self, action: #selector(showFriends),isBack:false)
        
        //注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
    }
    
}







